package com.gerfmarquez.nymph.music.core;

import com.gerfmarquez.nymph.TestNotesDurationActivity;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.util.Log;

public class PlayNotesThread extends Thread {
	
	 int noteType;
	 static final String TAG= "PlayNotesThread";
	 static byte[][] NOTES;
	 static byte[] rhythms;
	 
   	 
     int minBuffSize = AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
     AudioTrack audioTrack = new AudioTrack( AudioManager.STREAM_MUSIC,  44100,
 			AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_8BIT,
 			minBuffSize, AudioTrack.MODE_STREAM);
   	 
   	 public PlayNotesThread(int noteType, TestNotesDurationActivity tnda, byte[][] notesToPlay) {
		    audioTrack = new AudioTrack( AudioManager.STREAM_MUSIC,  44100,
					AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_8BIT,
					minBuffSize, AudioTrack.MODE_STREAM);
		 tnda.audioTracks.add(audioTrack);
   		 android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
   		 this.noteType = noteType;
   		NOTES = notesToPlay;
   		 Log.d(TAG, "Min Buffer size is "+minBuffSize);
		}
   	 public PlayNotesThread(byte[] rhythmsToPlay, TestNotesDurationActivity tnda, byte[][] notesToPlay) {
   		 audioTrack = new AudioTrack( AudioManager.STREAM_MUSIC,  44100,
   				 AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_8BIT,
   				 minBuffSize, AudioTrack.MODE_STREAM);
   		 tnda.audioTracks.add(audioTrack);
   		 android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
   		 NOTES = notesToPlay;
   		 Log.d(TAG, "Min Buffer size is "+minBuffSize);
   		rhythms = rhythmsToPlay; 
   	 }
   	
   	public void run() {

   		
   		byte[]  audByte;
   		int noteRhythm = noteType;
   		int songArrayIndex = 0;
   		int compass = 3;
   		int numberOfCompasses = 3;
   		int compassBytes = MusicCore.cutLength(noteRhythm);//nota que se va a reproducir
   		
   		AudioTrack aud = audioTrack;
   		byte []songArray = null;
   		int totalBytes = compassBytes*12; // son 12 notas las que tengo hardcodeadas y si son mas se cambia aqui
   		Log.d(TAG, "Total songArrayBytes = " + totalBytes);
   		songArray = new byte[totalBytes];//se tiene que corregir los bytes necesarios del songArray
   		for (int i = 0; i< NOTES.length; i++) {
   			int rhythm = 0;
   			if(rhythms != null) {
   				rhythm = rhythms[i];
   			}
   			else {
   				rhythm = noteType;
   			}
   			int bytesToWriteWithBPM = MusicCore.cutLength(rhythm);
   			audByte =  NOTES[i];
   		
   			Log.d(TAG, "Note number: "+i+" ");
   				
				songArray = MusicCore.append(songArray, audByte, songArrayIndex,bytesToWriteWithBPM);
				songArrayIndex += bytesToWriteWithBPM;
				
   			Log.d(TAG, "Bytes to write with bpm : " +bytesToWriteWithBPM);
   		}
   		
   		Log.d(TAG, "play back state is: " +aud.getPlayState());
   		aud.play();
   		Log.d(TAG, "play back state is: " +aud.getPlayState());
   		
   		int currentTempBuffIndex =  0 ;
   		while(currentTempBuffIndex < songArray.length) {
				byte[] tempBuff = new byte[minBuffSize];
				int i = 0;
				minBufferLoop:
				for(; i<minBuffSize; i++,currentTempBuffIndex++) {	
					if(currentTempBuffIndex >= songArray.length) {
						break minBufferLoop;
					}
					tempBuff[i] = songArray[currentTempBuffIndex];
				}
   				
   			aud.write(tempBuff, 0, i);
   		}
   		Log.d(TAG, "play back state is: " +aud.getPlayState());
   	}
}
